﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qkview.Models;

namespace qkview.Controllers
{
    public class NoteController : Controller
    {
        private QKVEntities db = new QKVEntities();

        //
        // GET: /Note/

        public ActionResult Index(string username,string boardurl, string edit = null)
        { 
            var board = db.Boards.FirstOrDefault(x => x.URLSlug == boardurl && x.UserProfile.UserName == username);
            if (board == null)
            {
                return HttpNotFound();
            }
            if (board.IsBlocked.HasValue && board.IsBlocked.Value)
            {
                ViewBag.Message = "This board is blocked, Please contact website administrator to unblock it";
                return View(viewName:"Message");
            }
            ViewBag.DisplayName = String.IsNullOrEmpty(board.UserProfile.Name) ? username : board.UserProfile.Name;
            ViewBag.UserName = username;           
            if (User.Identity.IsAuthenticated && User.Identity.Name == username && edit !=null)
            {
                //Use ApiController for data processing
                ViewBag.BoardID = Common.Encode(board.BoardID.ToString());
                ViewBag.BoardURL = board.URLSlug;
                ViewBag.BoardTitle = board.Title;
                ViewBag.BoardDescription = board.Description;
                ViewBag.IsEdit = true;
                return View();
            }
            else
            {
                //for SEO, direct put content on the page(Not from Ajax). 
                ViewBag.IsEdit = false;
                ViewBag.BoardTitle = board.Title;
                ViewBag.BoardDescription = board.Description;
                var notes = db.Notes.Include(n => n.Board).Include(x=>x.Theme).Where(x=>x.Board.BoardID  == board.BoardID);
                return View(notes.ToList());
            }
           
        }


        public ActionResult Section(string username, string boardurl, string sectionurl)
        {
            var section = db.Sections.FirstOrDefault(x => x.URLSlug == sectionurl && x.Note.Board.URLSlug == boardurl);
            if (section == null)
            {
                return HttpNotFound();
            }
            else {
                return View(model:section.Description);            
            }

        }



        //
        // GET: /Note/Details/5

        public ActionResult Details(int id = 0)
        {
            Note note = db.Notes.Find(id);
            if (note == null)
            {
                return HttpNotFound();
            }
            return View(note);
        }

        //
        // GET: /Note/Create

        public ActionResult Create()
        {
            ViewBag.BoardID = new SelectList(db.Boards, "BoardID", "Title");
            return View();
        }

        //
        // POST: /Note/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Note note)
        {
            if (ModelState.IsValid)
            {
                db.Notes.Add(note);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.BoardID = new SelectList(db.Boards, "BoardID", "Title", note.BoardID);
            return View(note);
        }

        //
        // GET: /Note/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Note note = db.Notes.Find(id);
            if (note == null)
            {
                return HttpNotFound();
            }
            ViewBag.BoardID = new SelectList(db.Boards, "BoardID", "Title", note.BoardID);
            return View(note);
        }

        //
        // POST: /Note/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Note note)
        {
            if (ModelState.IsValid)
            {
                db.Entry(note).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BoardID = new SelectList(db.Boards, "BoardID", "Title", note.BoardID);
            return View(note);
        }

        //
        // GET: /Note/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Note note = db.Notes.Find(id);
            if (note == null)
            {
                return HttpNotFound();
            }
            return View(note);
        }

        //
        // POST: /Note/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Note note = db.Notes.Find(id);
            db.Notes.Remove(note);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}